<?php
class GrabController extends BaseController {

    /**
     * 新增任务，标记为pending状态
     * URL:
     *      http://hehe.miku.video/grab/newtask?url=http://hehe.com/play.html&channel=unknown&udb=dw_hehe
     */
    public function actionNewTask(){
        $url = $this->arg('url');
        $channel = $this->arg('channel');
        $title = $this->arg('title', '暂无标题'.date('Ymd_H:i:s'));
        $udb = $this->arg('udb');
        $ret = obj('GrabTask')->addNewTask($url, $channel, $title, $udb);
        $this->jsonOutput($ret);
    }


    /**
     * 标记为成功
     * URL:
     *      http://hehe.miku.video/?r=grab/toggleSuccess&url=http://hehe.com/play12.html&vid=4243523
     */
    public function actionToggleSuccess(){
        $url = $this->arg('url');
        $vid = $this->arg('vid');
        $ret = obj('GrabTask')->toggleSuccess($url, $vid);
        $this->jsonOutput($ret);
    }


    /**
     * 标记为失败
     * URL:
     *      http://hehe.miku.video/?r=grab/toggleFailed&url=http://hehe.com/play12.html
     */
    public function actionToggleFailed(){
        $url = $this->arg('url');
        $ret = obj('GrabTask')->toggleFailed($url);
        $this->jsonOutput($ret);
    }


    //获取任务列表
    public function actionGetTaskList(){
        $status = $this->arg('status', 'all');
        $p = $this->arg('p', 1);
        $limit = $this->arg('limit', 20);
        $ret = obj('GrabTask')->getList($status, $p, $limit);
        $this->jsonOutput($ret);
    }


    //获取任务列表 - HTML
    public function actionTaskList(){
        $this->layout = null;
        $status = $this->arg('status', 'all');
        $p = $this->arg('p', 1);
        $limit = $this->arg('limit', 5);
        $ret = obj('GrabTask')->getList($status, $p, $limit);
        $this->list = $ret['list'];
        $this->pages = $ret['pages'];
        $this->status = $status;
        $this->jsonOutput(array('html'=>$this->display('grab_tasklist.html', true)));
    }


    //URL抓取规则获取
    public function actionGetUrlRule(){
        $c = file_get_contents(BASE_DIR.'/protected/data/url-rule.cf');
        $this->jsonOutput(array('rules' => $c));
    }


}